Fix crates with the same name as standard crates
authorAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 00:53:38 +0000 (17:53 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 23 Jul 2014 00:55:17 +0000 (17:55 -0700)
The test and bin executables weren't getting the correct --extern flags when
tested and built, so the names were conflicting. This passes --extern for the
local crate to ensure the right crate is picked up.

src/cargo/ops/cargo_new.rs
src/cargo/ops/cargo_rustc/mod.rs
tests/test_cargo_compile.rs
tests/test_cargo_test.rs

index 3039a5e3cab32958443054aa17738e1bad2996ef..51e8255d44b6f090113c217d832efcb4069f21e0 100644 (file)
@@ -1,12 +1,9 @@
 use std::os;
 use std::io;
-use std::io::{fs, File, Command};
+use std::io::{fs, File};
 
-use ops;
-use util::{CargoResult, human, ProcessError, Config, ChainError, process};
+use util::{CargoResult, human, ChainError, process};
 use core::shell::MultiShell;
-use core::source::Source;
-use sources::PathSource;
 
 macro_rules! git( ($($a:expr),*) => ({
     process("git") $(.arg($a))* .exec_with_output()
@@ -18,8 +15,7 @@ pub struct NewOptions<'a> {
     pub path: &'a str,
 }
 
-pub fn new(opts: NewOptions, shell: &mut MultiShell) -> CargoResult<()> {
-    let config = try!(Config::new(shell, false, None, None));
+pub fn new(opts: NewOptions, _shell: &mut MultiShell) -> CargoResult<()> {
     let path = os::getcwd().join(opts.path);
     if path.exists() {
         return Err(human(format!("Destination `{}` already exists",
index bba7072b2a18476ee828879d4974e9ac8fa938b0..4a17a61291d5c96ab7a162f0276a353cf314e650 100644 (file)
@@ -208,16 +208,16 @@ fn prepare_rustc(package: &Package, target: &Target, crate_types: Vec<&str>,
     let base = util::process("rustc").cwd(root.clone());
     let base = build_base_args(base, target, crate_types.as_slice());
 
-    let target = build_plugin_args(base.clone(), cx, false);
-    let plugin = build_plugin_args(base, cx, true);
-    let target = build_deps_args(target, package, cx, false);
-    let plugin = build_deps_args(plugin, package, cx, true);
+    let target_cmd = build_plugin_args(base.clone(), cx, false);
+    let plugin_cmd = build_plugin_args(base, cx, true);
+    let target_cmd = build_deps_args(target_cmd, target, package, cx, false);
+    let plugin_cmd = build_deps_args(plugin_cmd, target, package, cx, true);
 
     match req {
-        Target => vec![target],
-        Plugin => vec![plugin],
-        PluginAndTarget if cx.config.target().is_none() => vec![target],
-        PluginAndTarget => vec![target, plugin],
+        Target => vec![target_cmd],
+        Plugin => vec![plugin_cmd],
+        PluginAndTarget if cx.config.target().is_none() => vec![target_cmd],
+        PluginAndTarget => vec![target_cmd, plugin_cmd],
     }
 }
 
@@ -290,8 +290,8 @@ fn build_plugin_args(mut cmd: ProcessBuilder, cx: &Context,
     return cmd;
 }
 
-fn build_deps_args(mut cmd: ProcessBuilder, package: &Package, cx: &Context,
-                   plugin: bool) -> ProcessBuilder {
+fn build_deps_args(mut cmd: ProcessBuilder, target: &Target, package: &Package,
+                   cx: &Context, plugin: bool) -> ProcessBuilder {
     let layout = cx.layout(plugin);
     cmd = cmd.arg("-L").arg(layout.root());
     cmd = cmd.arg("-L").arg(layout.deps());
@@ -301,20 +301,40 @@ fn build_deps_args(mut cmd: ProcessBuilder, package: &Package, cx: &Context,
     cmd = push_native_dirs(cmd, &layout, package, cx, &mut HashSet::new());
 
     for &(_, target) in cx.dep_targets(package).iter() {
+        cmd = link_to(cmd, target, cx, true);
+    }
+
+    let mut targets = package.get_targets().iter().filter(|target| {
+        target.is_lib() && target.get_profile().is_compile()
+    });
+
+    if target.is_bin() {
+        for target in targets {
+            cmd = link_to(cmd, target, cx, false);
+        }
+    }
+
+    return cmd;
+
+    fn link_to(mut cmd: ProcessBuilder, target: &Target,
+               cx: &Context, is_dep_lib: bool) -> ProcessBuilder {
         let layout = cx.layout(target.get_profile().is_plugin());
         for filename in cx.target_filenames(target).iter() {
             let mut v = Vec::new();
             v.push_all(target.get_name().as_bytes());
             v.push(b'=');
-            v.push_all(layout.deps().as_vec());
+            if is_dep_lib {
+                v.push_all(layout.deps().as_vec());
+            } else {
+                v.push_all(layout.root().as_vec());
+            }
             v.push(b'/');
             v.push_all(filename.as_bytes());
             cmd = cmd.arg("--extern").arg(v.as_slice());
         }
+        return cmd;
     }
 
-    return cmd;
-
     fn push_native_dirs(mut cmd: ProcessBuilder, layout: &layout::LayoutProxy,
                         pkg: &Package, cx: &Context,
                         visited: &mut HashSet<PackageId>) -> ProcessBuilder {
index eabf75ac4a030a9e545d17e238c8e49525d0e7cc..cd88adab50b29e74d833b3f7a6a4a24dbf1cdf21 100644 (file)
@@ -1270,3 +1270,28 @@ test!(bad_cargo_toml_in_target_dir {
     assert_that(p.cargo_process("cargo-build"), execs().with_status(0));
     assert_that(process(p.bin("foo")), execs().with_status(0));
 })
+
+test!(lib_with_standard_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "syntax"
+            version = "0.0.1"
+            authors = []
+        "#)
+        .file("src/lib.rs", "
+            pub fn foo() {}
+        ")
+        .file("src/main.rs", "
+            extern crate syntax;
+            fn main() { syntax::foo() }
+        ");
+
+    assert_that(p.cargo_process("cargo-build"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} syntax v0.0.1 (file:{dir})
+",
+                       compiling = COMPILING,
+                       dir = p.root().display()).as_slice()));
+})
index 829ece783cee84e49cca7aa28a39caf674627c8a..769c8238196509b6dd7353fdc70b9df522065c7f 100644 (file)
@@ -390,3 +390,77 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured";
     assert!(out == format!("{}\n\n{}\n\n\n{}\n\n", head, bin, lib).as_slice() ||
             out == format!("{}\n\n{}\n\n\n{}\n\n", head, lib, bin).as_slice());
 })
+
+test!(lib_with_standard_name {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "syntax"
+            version = "0.0.1"
+            authors = []
+
+            [[lib]]
+            name = "syntax"
+            test = false
+        "#)
+        .file("src/lib.rs", "
+            pub fn foo() {}
+        ")
+        .file("tests/test.rs", "
+            extern crate syntax;
+
+            #[test]
+            fn test() { syntax::foo() }
+        ");
+
+    assert_that(p.cargo_process("cargo-test"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} syntax v0.0.1 (file:{dir})
+
+running 1 test
+test test ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
+                       ",
+                       compiling = COMPILING,
+                       dir = p.root().display()).as_slice()));
+})
+
+test!(lib_with_standard_name2 {
+    let p = project("foo")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "syntax"
+            version = "0.0.1"
+            authors = []
+
+            [[lib]]
+            name = "syntax"
+            test = false
+        "#)
+        .file("src/lib.rs", "
+            pub fn foo() {}
+        ")
+        .file("src/main.rs", "
+            extern crate syntax;
+
+            fn main() {}
+
+            #[test]
+            fn test() { syntax::foo() }
+        ");
+
+    assert_that(p.cargo_process("cargo-test"),
+                execs().with_status(0)
+                       .with_stdout(format!("\
+{compiling} syntax v0.0.1 (file:{dir})
+
+running 1 test
+test test ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n\n\
+                       ",
+                       compiling = COMPILING,
+                       dir = p.root().display()).as_slice()));
+})